<template>
<div>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="" prop="pass">
    <el-input type="text" v-model="ruleForm.pass" autocomplete="off" placeholder="请输入真实姓名"></el-input>
  </el-form-item>
  <el-form-item label="" prop="checkPass">
    <el-input type="text" v-model="ruleForm.checkPass" autocomplete="off" placeholder="二代身份证，请输入数字或字母"></el-input>
  </el-form-item>
  <el-form-item label="" prop="age">
    <el-input v-model.number="ruleForm.age" placeholder="请输入手机号码"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="button" @click="authentication()">提交认证</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
</div>
    
</template>
<script>
  export default {
    data() {
      var checkAge = (rule, value, callback) => {
        var phoneReg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
        if (!phoneReg.test(value)){
            callback(new Error('手机号码格式错误'));
        }
        if (!value) {
          return callback(new Error('手机号码不能为空'));
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('真实姓名不能为空'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        var idcardReg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9xX]$/;
        if (!idcardReg.test(value)){
            callback(new Error('身份证号码格式错误'));
        }
        if (value === '') {
          callback(new Error('身份证号码不能为空'));
        }else {
          callback();
        }
      };
      return {
        ruleForm: {
          pass: '',
          checkPass: '',
          age: ''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          age: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      // 提交认证
      authentication() {
            let user={}
                user.realname=this.ruleForm.pass,
                user.idcard=this.ruleForm.checkPass,
                user.phone=this.ruleForm.age
                  console.log(user)
                this.$axios.post('http://localhost:8080/authentication',user).then(response => {
                  if(response.data.code==1){
                      this.$router.push("/index");
                  }
            });
            console.log('error submit!!');
            return false;
          },
      
      // 清空输入框
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style scoped>

</style>